/*
 * This file is part of ddesktop: remote desktop system for iPad 
 *
 * Copyright (C) 2010 Kazuhiro Satoh
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, see <http://www.gnu.org/licenses/>.
 */


#pragma once
#pragma unmanaged

#include "stdafx.h"
#include "DesktopCapture.h"
#include "AbstractEncoder.h"
#include "DDServerPort.h"
#include "UIEventHandler.h"

#include <windows.h>


namespace DDesktopServer {

#define WM_CHANGE_SERVER_STATE  (WM_USER+1)

	class ServerController : public IDDServerPortCommandDelegator
	{
	private:
		HWND m_observer_window;
		DesktopCapture* m_capture;
		AbstractEncoder* m_encoder;
		DDServerPort* m_server_port;
		UIEventHandler* m_event_handler;
		unsigned int m_frame_ct;
		HANDLE m_capture_thread_handle;
		bool m_terminate_capture_thread;
		int m_frame_x, m_frame_y, m_frame_width, m_frame_height;
		int m_frame_zoom_scale;
		int m_image_quality;
		int m_image_resolution;

		void PostMessageToObservers(UINT msg, WPARAM wparam, LPARAM lparam);

		bool CreateCaptureThread(void);
		bool TerminateCaptureThread(void);
		static DWORD WINAPI CaptureAndSendThreadProc(LPVOID _self);
		static DWORD WINAPI CaptureTestThreadProc(LPVOID _self);

	public:

		ServerController(void);
		~ServerController();

		bool Init(int port);
		bool Close(void);
		void AddObserverWindow(HWND hwnd);

		const DDServerPort* GetDDServerPort(void) const;
		int GetFrameCt(void) const;
		int GetImageQuality(void)const;
		int GetImageResolution(void)const;
		void GetCaptureRect(RECT* rect);

		// server port commands
		virtual bool DDServerPortChangeState(void);
		virtual bool DDServerPortCommandSendImageHeader(
			int image_width, int image_height, int image_scale, const unsigned char **header_buf, int* header_length);
		virtual bool DDServerPortCommandUIEvent(DDUIEventType type, DDUIEventOwner owner, int param1, int param2);
		virtual bool DDServerPortCommandKeyinputs(char* buf, int len);
		virtual bool DDServerPortCommandSetDownloadFrame(int x, int y, int width, int height, int zoom_scale);
		virtual bool DDServerPortCommandDownloadFrame(int x, int y, int width, int height, char **result_buf, DWORD* result_len);
		virtual bool DDServerPortCommandChangeImageQuality(int quality);
		virtual bool DDServerPortCommandChangeImageResolution(int resolution);
	};
}


#pragma managed

